package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.salelogistics.basic.domain.BasBuildUnit;
import com.salelogistics.basic.domain.vo.BasBuildUnitVo;
import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.core.utils.StringUtils;


import com.salelogistics.common.security.utils.SecurityUtils;
import com.salelogistics.system.api.RemoteBasBuildUnitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.service.IBasBuildUnitService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 施工单位Controller
 *
 * @author ruoyi
 * @date 2023-07-09
 */
@RestController
@RequestMapping("unit")
public class BasBuildUnitController extends BaseController {
    //日志打印类
    private static final Logger log = LoggerFactory.getLogger(BasSupplierController.class);

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();
    @Autowired
    private IBasBuildUnitService basBuildUnitService;


    /**
     * 查询施工单位列表
     */
    @RequiresPermissions("basic:unit:list")
    @GetMapping("/list")
    public TableDataInfo list(BasBuildUnit basBuildUnit) {

        startPage();
        List<BasBuildUnit> list = basBuildUnitService.selectBasBuildUnitList(basBuildUnit);
        return getDataTable(list);
    }

    /**
     * 查询施工单位列表Vo
     */
    @RequiresPermissions("basic:unit:listVo")
    @GetMapping("/listVo")
    public TableDataInfo listVo(BasBuildUnit basBuildUnit) {
        List<BasBuildUnitVo> listVo=new ArrayList<>();
        startPage();
        List<BasBuildUnit> list = basBuildUnitService.selectBasBuildUnitList(basBuildUnit);
        for (int i = 0; i < list.size(); i++) {
            BasBuildUnitVo basBuildUnitVo = BeanUtil.toBean(list.get(i), BasBuildUnitVo.class);
            listVo.add(basBuildUnitVo);
        }
        return getDataTable(listVo);
    }

    /**
     * 导出施工单位列表
     */
    @RequiresPermissions("basic:unit:export")
    @Log(title = "施工单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasBuildUnit basBuildUnit) {
        List<BasBuildUnit> list = basBuildUnitService.selectBasBuildUnitList(basBuildUnit);
        ExcelUtil<BasBuildUnit> util = new ExcelUtil<BasBuildUnit>(BasBuildUnit.class);
        util.exportExcel(response, list, "施工单位数据");
    }

    /**
     * 获取施工单位详细信息
     */
    @RequiresPermissions("basic:unit:query")
    @GetMapping(value = "/{buildId}")
    public AjaxResult getInfo(@PathVariable("buildId") Long buildId) {
        BasBuildUnit basBuildUnit = basBuildUnitService.selectBasBuildUnitByBuildId(buildId);
        return success(basBuildUnit);
    }

//    /**
//     * 获取施工单位详细信息
//     */
//    @RequiresPermissions("basic:unit:query")
//    @GetMapping(value = "/{buildId}")
//    public AjaxResult getInfo(@PathVariable("buildId") Long buildId) {
//        BasBuildUnit basBuildUnit = basBuildUnitService.selectBasBuildUnitByBuildId(buildId);
//        BasBuildUnitVo basBuildUnitVo = BeanUtil.toBean(basBuildUnit, BasBuildUnitVo.class);
//        return success(basBuildUnitVo);
//    }

    /**
     * 新增施工单位
     */
    @RequiresPermissions("basic:unit:add")
    @Log(title = "施工单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasBuildUnit basBuildUnit) {
        if (checkParameterUtil.checkParameter(basBuildUnit, "basBuildUnit")) {
            basBuildUnit.setCreateBy(SecurityUtils.getUsername());
            basBuildUnit.setEditedBy(SecurityUtils.getUsername());
            return toAjax(basBuildUnitService.insertBasBuildUnit(basBuildUnit));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改施工单位
     */
    @RequiresPermissions("basic:unit:edit")
    @Log(title = "施工单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasBuildUnit basBuildUnit) {
        if (checkParameterUtil.checkParameter(basBuildUnit, "basBuildUnit")) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basBuildUnit.setEditedBy(SecurityUtils.getUsername());
            basBuildUnit.setEditedTime(currentTime);
            return toAjax(basBuildUnitService.updateBasBuildUnit(basBuildUnit));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 删除施工单位
     */
    @RequiresPermissions("basic:unit:remove")
    @Log(title = "施工单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{buildIds}")
    public AjaxResult remove(@PathVariable Long[] buildIds) {
        return toAjax(basBuildUnitService.deleteBasBuildUnitByBuildIds(buildIds));
    }
}
